<?php
/**
 * Created by PhpStorm.
 * User: zhubing
 * Date: 2019/1/25
 * Time: 9:57
 */
namespace app\wechat\common\model;
use think\Model;
use think\Db;

class Share extends Model{

    protected $table = 'et_mect_share';

    //文本替换
    public function replaceText($val, $param, $keys){

        $fis = '{{';
        $las = '}}';
        foreach($keys as $k=>$v){
            $str = $fis.$v.$las;
            $tmp = isset($param[$v]) ? $param[$v] : '';
            $val = str_replace($str, $tmp, $val);
        }
        return $val;
    }

    //获取弹窗数据
    public function partDialog($reads, $parts, $coups){

        //分享数据
        $share  = config('scene.share');
        $tempid = 't'.strval($parts['tempid']);
        $share  = isset($share[$tempid][$parts['shaid']]) ? $share[$tempid][$parts['shaid']] : $share[$tempid]['0'];
        $shjson = [];
        if($parts['shjson']){
            $shjson = json_decode($parts['shjson'],true);
        }
        $shjson = array_merge($share,$shjson);

        //分享数据处理
        $func1 = $parts['shaid'] ? 'share'.strval($parts['shaid']) : '';
        if($func1){
            $shjson = $this->$func1($shjson, $reads, $parts, $coups);
        }

        //互动数据
        $active = config('scene.active');
        $active = isset($active[$tempid][$parts['actid']]) ? $active[$tempid][$parts['actid']] : $active[$tempid]['0'];
        $acjson = [];
        if($parts['acjson']){
            $acjson = json_decode($parts['acjson'],true);
        }
        $dialog = array_merge($active,$acjson);
        $dialog['shareJs'] = $shjson;

        //场景数据处理
        $func2 = $parts['actid'] ? 'active'.strval($parts['actid']) : '';
        if($func2){
            $dialog = $this->$func2($dialog, $reads, $parts, $coups);
        }

        return $dialog;
    }

    //分享数据形参数据
    public function partDialogShareParam($reads, $parts, $coups){

        $param = [
            'title' => $parts['title'],
            'nickname' => $reads['nickname'],
            'headpic' => $reads['headpic'],
            'fromnickname' => $reads['fromnickname'],
            'fromheadpic' => $reads['fromheadpic'],
            'fromnm' => $reads['fromnm'],
            'nextPrize' => '',//下一张优惠券
            'number' => 0,//还差多少位
        ];

        //-1未达到领取条件、-2已达到领取条件、1已领取未使用、2已领取已使用、3已领取已过期
        if(!empty($coups)){
            foreach($coups as $k=>$val){
                if($val['status'] == -1){
                    $param['nextPrize'] = $val['title'];
                    $temp = $val['read'] - $param['fromnm'];
                    $param['balnum'] = $temp>0 ?  $temp : 1;
                    break;
                }
            }
        }
        return $param;
    }

    //分享场景1
    public function share1($shjson, $reads, $parts, $coups){

        $param = $this->partDialogShareParam($reads, $parts, false);
        $shjson['title'] = $this->replaceText($shjson['title'], $param, ['nickname','title']);
        return $shjson;
    }

    //互动场景1
    public function active1($dialog, $reads, $parts, $coups){

        $dialog['zhuliIs'] = 0;
        $dialog['mobileIs'] = 0;

        //是否出现助力弹窗
        if($reads['isadd'] && $reads['unionid'] != $reads['fromid']){
            $dialog['zhuliIs'] = 1;
        }

        //是否出现手机弹窗
        if(empty($reads['mobile'])){
            $dialog['mobileIs'] = 1;
            $dialog['mobileJs']['mobile'] = Db::name('mect_user')->where(['unionid'=>$reads['unionid']])->value('mobile');
        }
        return $dialog;
    }

}